//
//  HNAllJSExport.m
//  shezhan
//
//  Created by admin on 16/8/25.
//  Copyright © 2016年 haoniu. All rights reserved.
//

#import "HNAllJSExport.h"
#import "payRequsestHandler.h"
#import "Order.h"
#import "DataSigner.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#import "HNPaySuccessController.h"
//#import <ShareSDK/ShareSDK.h>
//#import <ShareSDKUI/ShareSDK+SSUI.h>


@interface HNAllJSExport ()

@end

@implementation HNAllJSExport

+(void)AliPayOrder:(NSString *)sign_content{
     NSString *appScheme = @"AliPay2088012528178653";
    [[AlipaySDK defaultService] payOrder:sign_content fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"使用支付宝的支付结果 %@",resultDic);
        
        
    }];
}


#pragma -mark AliPay
+(void)AliPayOrderName:(NSString *)orderName andOrderpPice:(NSString *)orderPrice andOrderNumber:(NSString *)orderNumber andOrderDesc:(NSString *)orderDesc{
    Order *order = [[Order alloc] init];
    order.partner = @"2088102169992904";
    order.seller = @"3423764851@qq.com";
    order.tradeNO = orderNumber; //订单ID（由商家?自?行制定）
    order.productName = orderName; //商品标题
    order.productDescription = [NSString stringWithFormat:@"订单号:%@",orderDesc]; //商品描述
    order.amount = orderPrice; //商品价格
    order.notifyURL = [NSString stringWithFormat:@"%@transaction/notify",url_Base]; //回调URL
    order.service = @"mobile.securitypay.pay";
    order.paymentType = @"1";
    order.inputCharset = @"utf-8";
    order.itBPay = @"30m";
    
    //应用注册scheme,在AlixPayDemo-Info.plist定义URL types
//    NSString *appScheme = [NSString stringWithFormat:@"alipay%@",[AppConfig getStringByUserConfigInfo:@"2088012009823463"]];
        NSString *appScheme = @"AliPay2016101902235982";
    
    //将商品信息拼接成字符串
    NSString *orderSpec = [order description];
    NSLog(@"orderSpec = %@",orderSpec);
    
    //获取私钥并将商户信息签名,外部商户可以根据情况存放私钥和签名,只需要遵循RSA签名规范,并将签名字符串base64编码和UrlEncode
    id<DataSigner> signer = CreateRSADataSigner(@"MIICXAIBAAKBgQC6MbPu1i2WyVNmLQr5EqhJA5Fq4n9Z70GCKkW25uxSLe1UNwIlj3I9euTta8AE2DVyRd/eoXSgKmP6DzJCPBPTbQjHjbj6mpijc5yLefR/ZZYrL3Op3QVucKyvmGynO65rOZBttzzgE9OKzEbRgneEFvy0WxF/REuD6Pgh9cFeGwIDAQABAoGAIIhssw+TOaSFwPWf1DNRWiiYcP+QVut6G789HR8uLZG+xRczddKhmGJvu5g3HeGVPRk+a4z0bsPZYijjlDUhFWS2aAt/fdMn+JFZHPHTBoEyLyH+cDkTfD+v/z0N52TZhiZYxuUZrGJs4qCoheG8M8NYLPhYZbTQRdGiB+duBOECQQDlItGimG2vytabHqhuMocSKipap5bba86rXfZbjqvg9SXSk4MibolXXwsR5wdiuhigd50o80slCFqmHfRmioERAkEA0AYMDZxUQJbD/EqBh6fwrNS7/iSqrlo3AY1BXPHp2uSqEyd0Yx4015EohugxzlEpB9vCHnsowq+kLV8SuMasawJAMizYuQEl67vAGMGy4kagO0MOeUF/kFbgL7horQ3l52HnQWBZ890+p+pBE0+C14SmxeTLfl8gUmCs8PlYPmgP8QJAGpFLJMnccMtQiH4zgNHQCSK6R8EDE3VuJQKXYHl89nnRxtR5JteKxCVD2cb55XG0Hc7RQfdUw1iPTUQhLfwhtQJBANscoWDvuuwRAssZOO8BZ0ypwDZWGlubxUYP31DdDyZhXy+IiZ+Q5K6wNISWn0qxma0xKoJZJEe477qe1Sy09AY=");
    NSString *signedString = [signer signString:orderSpec];
    NSLog(@"==signedString:%@",signedString);
    NSString *orderString = nil;
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    if (signedString != nil) {
        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
                       orderSpec, signedString, @"RSA"];
        
        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"使用支付宝的支付结果 %@",resultDic);
            
        }];
        
    }

}

+(void)WeiXinPayOrder:(NSDictionary *)dict{
    NSString *appId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"appid"]];
    NSString *nonceStr = [NSString stringWithFormat:@"%@",[dict objectForKey:@"noncestr"]];
    NSString *package = [NSString stringWithFormat:@"%@",[dict objectForKey:@"package"]];
    NSString *partnerId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"partnerid"]];
    NSString *sign = [NSString stringWithFormat:@"%@",[dict objectForKey:@"sign"]];
    NSString *prepayId = [NSString stringWithFormat:@"%@",[dict objectForKey:@"prepayid"]];
    UInt32 timeStamp = (UInt32)(UInt32)[[NSString stringWithFormat:@"%@",[dict objectForKey:@"timestamp"]] intValue];
    
    NSLog(@"%@",dict);
    
    //调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    
    req.openID              = appId;
    req.partnerId           = partnerId;
    req.prepayId            = prepayId;
    req.nonceStr            = nonceStr;
    req.timeStamp           = timeStamp;
    req.package             = package;
    req.sign                = sign;
//
    
//    req.openID              = @"wx7071e89271c2a9a9";
//    req.partnerId           = @"1430501802";
//    req.prepayId            = @"wx201702101555023ebec0bacf0827055097";
//    req.nonceStr            = @"JVSXcTNJEC1KCxCU";
//    req.timeStamp           =  (UInt32)(UInt32)[@"1486713302" intValue];
//    req.package             = @"Sign=WXPay";
//    req.sign                = @"ACE7EB775044A92FC74B2FE8FBC988C2";
    
    
    [WXApi sendReq:req];

}

//微信支付dict
+(void)WeiXinPayOrderName:(NSString *)orderName andOrderpPice:(NSString *)orderPrice andOrderNumber:(NSString *)orderNumber{
    NSLog(@"=======WeiXinPayOrderName");
    //向微信注册
    [WXApi registerApp:@"wx7071e89271c2a9a9"];
    
    //{{{
    //本实例只是演示签名过程， 请将该过程在商户服务器上实现
    
    //创建支付签名对象
    payRequsestHandler *req = [[payRequsestHandler alloc] init];
    //初始化支付签名对象
    [req init:@"wx7071e89271c2a9a9" mch_id:@"1430501802"];
    //设置密钥
    [req setKey:@"JfTbHYAzQifFIaASy1WP4dYZHmI1ORKq"];
    
    //}}}
    NSDictionary * dictCan = [NSDictionary dictionaryWithObjectsAndKeys:orderName,@"orderName",orderPrice,@"orderPrice",orderNumber,@"orderNo",[NSString stringWithFormat:@"%@transaction/notify",url_Base],@"notifyUrl", nil];
    //获取到实际调起微信支付的参数后，在app端调起支付
    NSMutableDictionary *dict = [req sendPay_demoByOrderDict:dictCan];//[req sendPay_demo];
    NSLog(@"=====dict:%@",dict);
    if(dict == nil){
        //错误提示
        NSString *debug = [req getDebugifo];
        NSLog(@"2%@\n\n",debug);
        NSArray * debugArray = [debug componentsSeparatedByString:@"return_msg"];
        if ([debugArray count]>0) {
            NSMutableString * retmsg = debugArray[1];
            retmsg = [NSMutableString stringWithFormat:@"%@",[retmsg substringFromIndex:1]];
            retmsg = [NSMutableString stringWithFormat:@"%@",[retmsg substringToIndex:retmsg.length-2]];
            //            [ProgressHUD showError:retmsg];
        }else{
            //            [ProgressHUD showError:@"支付错误"];
        }
    }else{
        NSLog(@"1%@\n\n",[req getDebugifo]);
        NSLog(@"dict:%@",dict);
        //[self alert:@"确认" msg:@"下单成功，点击OK后调起支付！"];
        NSMutableString *stamp  = [dict objectForKey:@"timestamp"];
        
        //调起微信支付
        PayReq* req             = [[PayReq alloc] init];
        req.openID              = [dict objectForKey:@"appid"];
        req.partnerId           = [dict objectForKey:@"partnerid"];
        req.prepayId            = [dict objectForKey:@"prepayid"];
        req.nonceStr            = [dict objectForKey:@"noncestr"];
        req.timeStamp           = stamp.intValue;
        req.package             = [dict objectForKey:@"package"];
        req.sign                = [dict objectForKey:@"sign"];
        
        [WXApi sendReq:req];
        
    }
}




@end
